package com.y2w.gulimall.seckill.controller;

import com.y2w.common.utils.R;
import com.y2w.gulimall.seckill.service.SeckillService;
import com.y2w.gulimall.seckill.to.SecKillSkuRedisTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class SeckillController {

    @Autowired
    SeckillService seckillService;

    /**
     * 返回当前时间可以参与的秒杀商品信息
     * @return
     */
    @GetMapping("/currentSeckillSkus")
    @ResponseBody
    public R getCurrentSeckillSkus(){
        List<SecKillSkuRedisTo> vos=seckillService.getCurrentSeckillSkus();
        if(vos!=null&&vos.size()>0){
            return R.ok().setData(vos);
        }else{
            return R.error("未查询出结果");
        }
    }
    @ResponseBody
    @GetMapping("/sku/seckill/{skuId}")
    public R getSkuSeckillInfo(@PathVariable("skuId") Long skuId){
        SecKillSkuRedisTo to=seckillService.getSkuSeckillInfo(skuId);
        if(to!=null){
            return R.ok().setData(to);
        }else{
            return R.error("无此商品预约活动");
        }

    }


    @GetMapping("/kill")
    public String secKill(@RequestParam("killId") String killId,
                     @RequestParam("key") String key,
                     @RequestParam("num") Integer num,
                          Model model){
        String orderSn=seckillService.kill(killId,key,num);
        model.addAttribute("orderSn",orderSn);
        return "success";
    }
}
